package com.team.kgc.grade.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.kgc.grade.entity.Student;
import com.team.kgc.grade.entity.StudentCondtion;
import com.team.kgc.grade.entity.StudentExample;
import com.team.kgc.grade.mapper.GradeMapper;
import com.team.kgc.grade.mapper.StudentMapper;
import com.team.kgc.grade.service.GradeService;
import com.team.kgc.grade.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import java.util.List;
import java.util.Map;

@Service(value = "studentService") //表示业务组件  组件id为"studentSerivce"
 @Transactional//支持事务,要么成功,要么不成功
public class StudentServiceImpl implements StudentService{
    //声明dao层接口属性
    //使用@Autowired进行自动注入 默认按类型
    //从容器中找相应接口的对象进行自动赋值

    @Autowired    //@Autowired表示自动装配
     private StudentMapper studentMapper;
    @Autowired
     private GradeMapper gradeMapper;

    @Transactional(propagation= Propagation.NOT_SUPPORTED)
    public List<Student> getAllStudent(){
        return this.studentMapper.selectStuByGrade();
    }

    public int deleteStudent(Integer xh) {
        return this.studentMapper.deleteByPrimaryKey(xh);
    }

    public void deleteStudentByGid(Integer gid) {
        studentMapper.deleteStudentByGid(gid);
        gradeMapper.deleteByPrimaryKey(gid);
    }

    public int addStudent(Student stu) {
        return this.studentMapper.insertSelective(stu);
    }

    @Transactional(propagation= Propagation.NOT_SUPPORTED)
    public Student selectOneStu(Integer xh) {
        return this.studentMapper.selectByPrimaryKey(xh);
    }
    
    public int updateStu(Student stu) {
        return this.studentMapper.updateByPrimaryKeySelective(stu);
    }

//    @Override
//    public PageInfo<Student> selectStuByPage(Integer page, Integer pageSize) {
////使用PageHelper插件进行分页
//        //1.导入pageHelper的依赖
//        //2.在mybatis配置文件中配置pageHelper分页的插件
//        //3.开启分页
//        PageHelper.startPage(page,pageSize);
//        //4.调用dao层查询所有学生信息执行sql:select * from student
//        //当前开启分页后，插件就会拦截执行的查询语句并进行修改实现分页
//        //select * from student limit  (page-1*页大小,页大小
//        List<Student>li=studentMapper.selectByExample(new StudentExample());
//        //5.创建PageInfo对象封装分页相关的信息
//        PageInfo<Student>pageInfo=new PageInfo<>(li);
//        return pageInfo;
//    }

    @Override
    public PageInfo<Student> selectStuByCondtion(StudentCondtion sc) {

        PageHelper.startPage(sc.getPage(),sc.getPageSize());
        List<Student>list=this.studentMapper.selectStuByCondtion(sc);
        return new PageInfo<Student>(list);
    }


}
